/* Copyright 2007 Ryan Phillips.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdarg.h>
#include "xblf.h"

void xblf_printf(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vprintf(fmt, ap);
    va_end(ap);
}

void banner()
{
    fprintf(stderr, "xblftest %s\n", XBLF_VERSION_SHORT);
}

void usage()
{
    fprintf(stderr, "usage: xblftest [xml file]\n");
}

int main(int argc, const char *argv[])
{
    xblf_friend_t *f;
    char *value;
    int ivalue;
    int is_online;
    int i;
    xblf_error_t rv;

    banner();

    if (argc < 2) {
        usage();
        return 1;
    }

    xblf_init();

    f = xblf_friend_new_by_tag("trolocsis1");
    f->debug_filename = argv[1];

    rv = xblf_friend_query(f);
    if (!rv) {
        fprintf(stderr, "xblf_friend_query returned %i\n", (int) rv);
        goto cleanup;
    }

    xblf_recentgames_t **recent_games;

    printf("Account Type: ");
    switch(xblf_friend_get_account_status(f)) {
    case XBLF_ACCOUNT_GOLD:
        xblf_printf("Gold\n");
        break;
    case XBLF_ACCOUNT_SILVER:
        xblf_printf("Silver\n");
        break;
    case XBLF_ACCOUNT_UNKNOWN:
        xblf_printf("Unknown\n");
        break;
    }

    ivalue = xblf_friend_get_gamerscore(f);
    xblf_printf("GamerScore:    %i\n", ivalue);

    value = xblf_friend_get_profileurl(f);
    xblf_printf("ProfileUrl:    %s\n", value);
    free(value);

    value = xblf_friend_get_tileurl(f);
    xblf_printf("TileUrl:       %s\n", value);
    free(value);

    value = xblf_friend_get_reputationurl(f);
    xblf_printf("ReputationUrl: %s\n", value);
    free(value);

    is_online = xblf_friend_presence_get_online(f);
    xblf_printf("Online:        %s\n", (is_online == 1)?"online":"offline");

    if ((recent_games=xblf_friend_get_recentgames(f)) != NULL) {
        for (i=0; recent_games[i]; i++) {
            xblf_recentgames_t *recent_game = recent_games[i];
            xblf_printf("Game: Name : %s\n", recent_game->game.name);
        }
    }
    xblf_friend_recentgames_free(recent_games);

cleanup:
    xblf_friend_free(f);
    xblf_finish();

    return (int)rv;
}
